// JavaScript Document
(function($){
    $.fn.slider = function(options)
	{
		var defaults ={
			effect: 'rightScroll',//fedeAuto 透明度, leftScroll 左滚, rightScroll 右滚, move 
			imgHeight: 337 
		};
		var opts = $.extend({}, defaults, options); 
		var $this = $(this);
		var iNow = 0;
		var bgLength = 20;
		var j = 0;
		var urlArray = [];
		var iNowZindex = 2;
		var sliderNow = 0;
		var timer = null;

		$this.css({height: opts.imgHeight});
		$this.find('.pic_box li img').css({height: opts.imgHeight});
		//cffect
		var effect = {};
			//fade
		effect.fade= {
			fadeAutoStyle: function(){
				$('.pic_box').css({
					height: opts.imgHeight
				});
				$('.pic_box li').css({ 
					height: opts.imgHeight,
					display: 'none',
					position: 'absolute'
				});
				$('.pic_box li').eq(0).css({
					display: 'block'
				});
			},
			fedeAuto:function ()
			{
				$this.find('li').fadeOut(350);
				$this.find('li').eq(iNow).fadeIn(200);
			}
		}
			//scroll
		effect.scroll={
			leftScrollStyle:function(){
				$('.pic_box').css({
					width:$('.pic_box li').length*$('.pic_box li').width()+'px', 
					height: opts.imgHeight,
					position: 'absolute'
				});
				$('.pic_box li').css({ 
					height: opts.imgHeigh,
					float: 'left'
				});
			},
			leftScrollMove:function(){
				$('.pic_box').animate({
					left: -iNow*$('.pic_box li').width()
				},'slow');
			},
			rightScrollStyle:function(){
				iNow = $this.find('li').length;
				$('.pic_box').css({
					width:$('.pic_box li').length*$('.pic_box li').width()+'px', 
					height: opts.imgHeight,
					left: -$('.pic_box').width()*($('.pic_box li').length-1),
				});
				$('.pic_box li').css({height: opts.imgHeight, float: 'left'});
			},
			rightScrollMove:function(){
				$('.pic_box').animate({
					left: -iNow*$('.pic_box li').width()
				},'slow');
			}
		}
		effect.move={
			moveStyle:function(){
				for(var i=0;i<$this.children('.pic_box').children('li').length;i++)
				{
					urlArray.push($this.children('.pic_box').children('li').eq(i).children('a').children('img').attr('src'));
				}
				for(j = 0;j<$this.children('.pic_box').children('li').length;j++)
				{
					

					$this.children('.pic_box').children('li').eq(j).find('a').remove();
					$this.children('.pic_box').children('li').eq(j).append('<div class="bigPicture"><ul></ul></div>');
					for(var i = 0;i<bgLength;i++)
					{
						$this.children('.pic_box').children('li').eq(j).find('.bigPicture ul').append('<li></li>');
						$this.children('.pic_box').children('li').eq(j).find('.bigPicture ul li').eq(i).css({
							width:50, height:337, 
							left:i*$this.children('.pic_box').children('li').eq(j).find('.bigPicture ul li').width(),
							position:'absolute',
							float:'left', 
							zIndex:1,
							'background': 'url('+urlArray[j]+') '+'-'+i*$this.find('.bigPicture ul li').width()+'px 0'
						});
						if(i%2==0)
						{
							$this.children('.pic_box').children('li').eq(j).find('.bigPicture ul li').eq(i).css({top:-337});
						}else{
							$this.children('.pic_box').children('li').eq(j).find('.bigPicture ul li').eq(i).css({top: 337});
						}
					}
				}

				timer = setInterval(function(){
					$this.children('.pic_box').children('li').eq(iNow).find('.bigPicture ul li').eq(sliderNow).animate({top:0}, 'slow')
					sliderNow++
					if(sliderNow>=bgLength)
					{
						sliderNow = 0;
						clearInterval(timer);
					}
				},30)
				
			},
			moveAtuo:function(){
				for(var i=0;i<bgLength;i++)
				{
					if(i%2==0)
					{
						$this.children('.pic_box').children('li').eq(iNow).find('.bigPicture ul li').eq(i).css({top:-337});
					}else{
						$this.children('.pic_box').children('li').eq(iNow).find('.bigPicture ul li').eq(i).css({top: 337});
					}
				}
				$this.children('.pic_box').children('li').eq(iNow).find('.bigPicture ul li').css({
					zIndex:iNowZindex++
				})
				timer = setInterval(function(){
					$this.children('.pic_box').children('li').eq(iNow).find('.bigPicture ul li').eq(sliderNow).animate({top:0}, 'slow')
					sliderNow++;
					if(sliderNow>=bgLength)
					{
						sliderNow = 0;
						clearInterval(timer);
					}
				},30)
			}
		}
		switch(opts.effect)
		{
			case 'fedeAuto':
				effect.fade.fadeAutoStyle();
				break;
			case 'leftScroll':
				effect.scroll.leftScrollStyle();
				break;
			case 'rightScroll':
				effect.scroll.rightScrollStyle();
				break;
			case 'move':
				effect.move.moveStyle();
				break;
		}

		setInterval(function(){
			switch(opts.effect)
			{
				case 'fedeAuto':
					iNow++;
					if(iNow>=$this.find('li').length)
					{
						iNow = 0;
					};

					effect.fade.fedeAuto();
					break;
				case 'leftScroll':
					iNow++;
					if(iNow>=$this.find('li').length)
					{
						iNow = 0;
					};

					effect.scroll.leftScrollMove();
					break;
				case 'rightScroll':
					iNow--;
					effect.scroll.rightScrollMove();
					if(iNow==0)
					{
						iNow = $this.find('li').length;
					};
					break;
				case 'move':
					iNow++;
					if(iNow>=$this.children('.pic_box').children('li').length)
					{
						iNow = 0;
					};
					effect.move.moveAtuo();
					break;
			}
		},5000);
	};
})(jQuery);